package com.example.xss.surfaceviewtest.myview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * 通过子线程实现页面刷新
 * 适用自定义View频繁刷新或刷新时处理的数据量较大
 */
public class MySurfaceView extends SurfaceView
        implements SurfaceHolder.Callback, Runnable {
    private static final String TAG = "MySurfaceView";
    private Context mContext;

    private SurfaceHolder surfaceHolder;
    private Canvas canvas;//绘图
    private boolean isDrawing = false;//控制用于绘制的子线程
    private Path mPath;
    private Paint mPaint;

    public MySurfaceView(Context context) {
        this(context, null);
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init();
    }

    private void init() {
        surfaceHolder = getHolder();//获取surfaceHolder
        surfaceHolder.addCallback(this);//注册回调
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setKeepScreenOn(true);

        mPath = new Path();
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(40);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.i(TAG, "surfaceCreated: ");
        isDrawing = true;
        new Thread(this).start();//开启子线程进行绘制
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.i(TAG, "surfaceChanged: ");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

        isDrawing = false;
        Log.i(TAG, "surfaceDestroyed: ");
    }

    @Override
    public void run() {
        long start = System.currentTimeMillis();
        while (isDrawing) {    //使用while循环实现不停绘制
            draw();
        }
        long end = System.currentTimeMillis();
        //50-100ms
        if (end - start < 100) {
            try {
                Thread.sleep(100 - (end - start));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @SuppressLint("ResourceAsColor")
    private void draw() {
        //获取当前canvas对象
        //获取到的Canvas还是继续上次的Canvas对象,而不是一个新的 Canvas 对象
        // 因此,之前的绘图操作都会被保留
        // 如果需要擦除,则可以在绘制前,通过drawColor()方法来进行清屏操作
        try {
            canvas = surfaceHolder.lockCanvas();
            //TODO draw someting
            if (null != canvas) {
                canvas.drawColor(Color.WHITE);
                canvas.drawPath(mPath, mPaint);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != canvas) {
                //提交画布内容
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPath.moveTo(x, y);
                break;
            case MotionEvent.ACTION_MOVE:
                mPath.lineTo(x, y);
                break;
            case MotionEvent.ACTION_UP:
                break;
            default:
                break;
        }
        return true;
    }

    //清空面板
    public void clear() {
        mPath = new Path();
    }
}
